const service = require('./../service');
const logger = require('./../commons/logger');
exports = module.exports = {
  getAppointments: async (ctx, next) => {
    let params = ctx.params || {};
    let query = ctx.query || {};

    logger.info('getAppointments', params);
    try {
      let result = {
        code: 10000,
        msg: '获取课程列表成功',
        data: {
          page: {
            number: 0,
            size: 20,
            total: 0
          },
          data: []
        }
      };

      if (params.code && params.code.length > 0) {

        let person = await service.getUserInfo(ctx, params.code);

        if (person.code === 10000) {
          person.data.personId = '115d828092e011e788f1a3cf2e185a0e';
          if (person.data && person.data.personId && person.data.personId.length > 0) {
            ctx.session.appoint = {
              q: {
                page: {
                  size: 20,
                  number: 0
                },
                query: {
                  personId: person.data.personId,
                  periodId: query.periodId
                }
              }
            }
            let appoints = await service.getAppointments(ctx, ctx.session.appoint);
            if (appoints.code !== 10000) {
              throw new BSPError(record);
            } else {
              logger.debug('getAppointments', appoints.data);
              result = appoints;
            }
          }
        }
      }

      ctx.status = 200;
      ctx.body = result;
    } catch (e) {
      logger.error('getAppointments Error', e);
      ctx.status = 500;
      ctx.body = { code: e.name, msg: e.message };
    }

  },
  moreAppoints: async (ctx, next) => {
    let query = ctx.session.appoint || {};
    
    logger.info('moreAppoints', query);
    try {
      query.q.page.number = query.q.page.number + 1;
      let appoints = await service.getAppointments(ctx, query);
      if (appoints.code !== 10000) {
        throw new BSPError(appoints);
      } else {
        logger.debug('moreAppoints', appoints.data);
        ctx.status = 200;
        ctx.body = { code: 10000, msg: '获取课程列表成功', data: appoints.data };
      }
    } catch (e) {
      logger.error('moreAppoints Error', e);
      ctx.status = 500;
      ctx.body = { code: e.name, msg: e.message };
    }
  },
  doAppointment: (ctx, next) => {

    let person = ctx.session.person&&ctx.session.person.data || {};

    if (!person.mobile || person.mobile.length <= 0) {
      ctx.status = 200;
      ctx.body = { code: 10000, msg: '请绑定手机号码', step: -1 };
    } else if (person.role !=='2' && person.role !== '3') {
      ctx.status = 500;
      ctx.body = { code: 10403, msg: '只有决策人主决策人可以报名' };
    } else {
      ctx.status = 200;
      ctx.body = {
        code: 10000,
        msg: '请输入用户姓名',
        step: 1,
        data: person
      };
    }
  },
  updateName: async(ctx, next) => {
    let params = ctx.request.body.name || '';
    
    logger.info('updateName', params);
    try {
      let ret = await service.updateName(ctx, params);
      if (ret.code !== 10000) {
        throw new BSPError(ret);
      } else {
        logger.debug('updateName', ret.data);
        ctx.status = 200;
        ctx.body = { code: 10000, msg: '更新名称成功', data: ret.data };
      }
    } catch (e) {
      logger.error('updateName Error', e);
      ctx.status = 500;
      ctx.body = { code: e.name, msg: e.message };
    }
  },
  getContacts: async(ctx, next) => {
    let params = ctx.request.body || {};
    
    logger.info('getContacts', params);
    try {
      let ret = await service.getContacts(ctx, params);
      if (ret.code !== 10000) {
        throw new BSPError(ret);
      } else {
        logger.debug('getContacts', ret.data);
        ctx.status = 200;
        ctx.body = { code: 10000, msg: '获取课程列表成功', data: ret.data };
      }
    } catch (e) {
      logger.error('getContacts Error', e);
      ctx.status = 500;
      ctx.body = { code: e.name, msg: e.message };
    }
  },
  newAppointment: async(ctx, next) => {
    let params = ctx.request.body || {};

    logger.info('newAppointment', params);
    try {
      let ret = await service.newAppointment(ctx, params);
      if (ret.code !== 10000) {
        throw new BSPError(ret);
      } else {
        logger.debug('newAppointment', ret.data);
        ctx.status = 200;
        ctx.body = { code: 10000, msg: '预约成功', data: ret.data };
      }
    } catch (e) {
      logger.error('newAppointment Error', e);
      ctx.status = 500;
      ctx.body = { code: e.name, msg: e.message };
    }
  },
  getAccommodation: async(ctx) => {
    let id = ctx.params.id || '';

    logger.info('getAccommodation', id);
    try {
      let ret = await service.getAccommodation(ctx, id);
      if (ret.code !== 10000) {
        throw new BSPError(ret);
      } else {
        logger.debug('getAccommodation', ret.data);
        ctx.status = 200;
        ctx.body = { code: 10000, msg: '查询住宿成功', data: ret.data };
      }
    } catch (e) {
      logger.error('getAccommodation Error', e);
      ctx.status = 500;
      ctx.body = { code: e.name, msg: e.message };
    }
  }
}